package com.etl.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eksframework.commons.core.base.EksCheck;
import com.eksframework.commons.core.base.EksCopier;
import com.eksframework.commons.core.request.PageParam;
import com.eksframework.commons.json.EksJson;
import com.etl.sys.entity.SysCinema;
import com.etl.sys.entity.SysTenant;
import com.etl.sys.service.ISysCinemaService;
import com.etl.sys.service.ISysTenantService;
import com.etl.sys.vo.CinemaParam;
import com.etl.sys.vo.CinemaVO;
import com.google.common.base.Functions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 影院表 前端控制器
 * </p>
 *
 * @author chenfenghai
 * @since 2020-11-03
 */
@Slf4j
@RestController
@RequestMapping("/cinema")
public class SysCinemaController {

    @Autowired
    ISysTenantService sysTenantService;
    @Autowired
    ISysCinemaService sysCinemaService;

    @PostMapping("/page")
    public IPage<CinemaVO> page(@RequestBody CinemaParam params) {
        log.debug("page: {}", EksJson.toJson(params));
        QueryWrapper<SysCinema> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(params.getTenantId()) && params.getTenantId() > 0) {
            queryWrapper.eq("TENANT_ID", params.getTenantId());
        }
        if (StringUtils.isNoneBlank(params.getCinemaCode())) {
            queryWrapper.like("CINEMA_CODE", params.getCinemaCode());
        }
        if (StringUtils.isNoneBlank(params.getCinemaName())) {
            queryWrapper.like("CINEMA_NAME", params.getCinemaName());
        }
        IPage<SysCinema> page = new Page<>(
                params.getPageNumber(), params.getPageSize(), params.getTotal());
        page = sysCinemaService.page(page, queryWrapper);
        if(CollectionUtils.isEmpty(page.getRecords())){
            return new Page<>(params.getPageNumber(), params.getPageSize(), params.getTotal());
        }
        List<Long> tenantIdList = page.getRecords().parallelStream()
                .map(SysCinema::getTenantId).distinct().collect(Collectors.toList());
        Map<Long, SysTenant> tenantMap = sysTenantService.listByIds(tenantIdList)
                .stream().collect(Collectors.toMap(SysTenant::getTenantId, Functions.identity()));
        return page.convert(sysCinema -> {
            CinemaVO cinemaVO = EksCopier.copy(sysCinema, CinemaVO.class);
            SysTenant sysTenant = tenantMap.get(sysCinema.getTenantId());
            cinemaVO.setTenantCode(sysTenant.getTenantCode());
            cinemaVO.setTenantName(sysTenant.getTenantName());
            return cinemaVO;
        });
    }

    @GetMapping("/list")
    public List<SysCinema> list(){
        return sysCinemaService.list();
    }

    @PostMapping("/save")
    public void save(@Validated @RequestBody CinemaVO cinemaVO) {
        if (Objects.isNull(cinemaVO.getCinemaId()) || cinemaVO.getCinemaId() <= 0) {
            cinemaVO.setCinemaId(null);
            Integer count = sysCinemaService.lambdaQuery()
                    .eq(SysCinema::getCinemaCode, cinemaVO.getCinemaCode())
                    .count();
            if (count > 0) {
                throw new IllegalArgumentException("影院编码不能重复，请重新填写");
            }
        }
        SysCinema sysCinema = EksCopier.copy(cinemaVO, SysCinema.class);
        sysCinemaService.saveOrUpdate(sysCinema);
    }

    @DeleteMapping("/del")
    public void del(@RequestParam(name = "cinemaId") Long cinemaId) {
        sysCinemaService.lambdaUpdate()
                .set(SysCinema::getDeleteFlag, cinemaId)
                .eq(SysCinema::getCinemaId, cinemaId)
                .update();
    }
}
